package com.google.project.client.HumanResources;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.project.client.Service.TaskRequireService;
import com.google.project.client.Service.TaskRequireServiceAsync;
import com.google.project.shared.CommonFuntion;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectTaskDTO;
import com.google.project.shared.TaskRequireDTO;
import com.google.gwt.user.client.ui.Hyperlink;

public class HMHumanReqDetail extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField(provided = true)
	CellTable<TaskRequireDTO> ctbProjectTask = new CellTable<TaskRequireDTO>();
	@UiField SimplePager pager;
	@UiField Label lblProjectName;
	@UiField Label lblNumOfEmp;
	@UiField Button btnBack;
	@UiField Label lblProjectDate;
	@UiField Hyperlink lblNumOfOPT;
	interface Binder extends UiBinder<Widget, HMHumanReqDetail> {
	}
	private final TaskRequireServiceAsync taskReqSv = GWT.create(TaskRequireService.class);
	ProjectTaskDTO taskDTO = new ProjectTaskDTO();
	ProjectDTO currProject = new ProjectDTO();

	public HMHumanReqDetail() {
		initWidget(binder.createAndBindUi(this));
	}

	// contructor
	public HMHumanReqDetail(ProjectDTO projectDTO) {
		this.currProject = projectDTO;
		
		initWidget(binder.createAndBindUi(this));
		
		lblProjectDate.setText("("
				+ CommonFuntion.convertToDate(new Date(currProject
						.getStartDate()))
				+ " - "
				+ CommonFuntion
						.convertToDate(new Date(currProject.getDueDate()))
				+ ")");
		lblProjectName.setText("Dự án: " + currProject.getProjectName());
		lblNumOfOPT.setText(currProject.getNumOfOPT()+"");
		lblNumOfEmp.setText(currProject.getNumOfEmpReq() + " (Nhân viên)");
		initTable();
		bindData(currProject.getProjectID());
	}

	/***
	 * lấy về tất cả task con ko cha theo projectId
	 */
	public void bindData(final String projectId) {
		try {
			taskReqSv.getListTaskReq (new AsyncCallback<List<TaskRequireDTO>>() {
				
				@Override
				public void onSuccess(List<TaskRequireDTO> result) {
					List<TaskRequireDTO> temp = new ArrayList<TaskRequireDTO>();
					for (TaskRequireDTO taskRequireDTO : result) {
						if(taskRequireDTO.getProjectId().equals(projectId)) {
							temp.add(taskRequireDTO);
						}
					}
					loadData(temp);
				}
				
				@Override
				public void onFailure(Throwable caught) {
				}
			});
		} catch (Exception ex) {
			ex.getMessage();
		}

	}

	// Ham tao Cell table
	public void initTable() {
		//STT
		TextColumn<TaskRequireDTO> indexColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO object) {
				return object.getIndex()+"";
			}
		};
		//Số lượng
		Column<TaskRequireDTO, String> numOfEmpColumn = new Column<TaskRequireDTO, String>(
				new ClickableTextCell()) {
			@Override
			public String getValue(TaskRequireDTO object) {
				return object.getNumOfEmp() + " (Nhân viên)";
			}
		};
		numOfEmpColumn.setFieldUpdater(new FieldUpdater<TaskRequireDTO, String>() {
					@Override
					public void update(int index,
							TaskRequireDTO projectTaskDTO, String value) {
						HMEntrypoint.get().HMStaffForAddProject(projectTaskDTO,currProject);
					}
				});
		//đã đáp ứng
		TextColumn<TaskRequireDTO> NumOfResponse = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO object) {
				return object.getNumOfResponse()+  " (Nhân viên)";
			}
		};
		//Chuyen mon
		TextColumn<TaskRequireDTO> specColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO object) {
				return object.getSpecializeName();
			}
		};
		
		//Kinh nghiem
		TextColumn<TaskRequireDTO> expColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO object) {
				int exp = object.getExperience();
				String result = "";
				if (exp == 1) {
					result += " < 1 năm";
				} else if (exp == 2) {
					result += "1-3 năm";
				} else if (exp == 3) {
					result += "3-5 năm";
				} else if (exp == 4) {
					result += "5-10 năm";
				} else if (exp == 5) {
					result += " > 10 năm";
				}
				return result;
			}
			
		};
		//đã đáp ứng
		TextColumn<TaskRequireDTO> taskNameColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO object) {
				return object.getTaskName();
			}
		};
		//Ngay bat dau
		TextColumn<TaskRequireDTO> startDateColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO object) {
				return CommonFuntion.convertToDate(new Date(object.getStartDate()));
			}
		};
		
		//Ngay ket thuc
		TextColumn<TaskRequireDTO> dueDateColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO object) {
				return CommonFuntion.convertToDate(new Date(object.getDueDate()));
			}
		};

		ctbProjectTask.setWidth("100%", true);
		ctbProjectTask.setPageSize(5);
		ctbProjectTask.setPageStart(0);
		ctbProjectTask.setStyleName("ttable");

		ctbProjectTask.addColumn(indexColumn, "STT");
		ctbProjectTask.setColumnWidth(indexColumn, 30.0, Unit.PX);

		

		ctbProjectTask.addColumn(numOfEmpColumn, "Yêu cầu");
		ctbProjectTask.setColumnWidth(numOfEmpColumn, 120.0, Unit.PX);
		numOfEmpColumn.setCellStyleNames("celltable");
		
		ctbProjectTask.addColumn(NumOfResponse, "Đáp ứng");
		ctbProjectTask.setColumnWidth(NumOfResponse, 120.0, Unit.PX);
		
		ctbProjectTask.addColumn(specColumn, "Chuyên môn");
		ctbProjectTask.setColumnWidth(specColumn, 20.0, Unit.PCT);
		
		ctbProjectTask.addColumn(expColumn, "Kinh nghiệm");
		ctbProjectTask.setColumnWidth(expColumn, 20.0, Unit.PCT);
		ctbProjectTask.getColumn(ctbProjectTask.getColumnIndex(expColumn)).setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
	
		
		ctbProjectTask.addColumn(startDateColumn, "Từ ngày");
		ctbProjectTask.setColumnWidth(startDateColumn, 20.0, Unit.PCT);
		ctbProjectTask.getColumn(ctbProjectTask.getColumnIndex(startDateColumn)).setHorizontalAlignment(HasAlignment.ALIGN_LEFT);

		ctbProjectTask.addColumn(dueDateColumn, "Đến ngày");
		ctbProjectTask.setColumnWidth(dueDateColumn, 20.0, Unit.PCT);
		ctbProjectTask.getColumn(ctbProjectTask.getColumnIndex(dueDateColumn)).setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
		
//		ctbProjectTask.addColumn(taskNameColumn, "Tên công việc");
//		ctbProjectTask.setColumnWidth(taskNameColumn, 40.0, Unit.PCT);
//		taskNameColumn.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
	}

	public void loadData(List<TaskRequireDTO> projectTaskList) {
		sortProjectTaskDTO(projectTaskList);
		// Create a data provider.
		ListDataProvider<TaskRequireDTO> dataProvider = new ListDataProvider<TaskRequireDTO>();

		int index = 1;
		List<TaskRequireDTO> list = dataProvider.getList();
		for (TaskRequireDTO projectTaskDTO : projectTaskList) {
			projectTaskDTO.setIndex(index);
			list.add(projectTaskDTO);
			index++;
		}
		pager.setDisplay(ctbProjectTask);
		dataProvider.addDataDisplay(ctbProjectTask);
	}
	private void sortProjectTaskDTO(List<TaskRequireDTO> listTaskDTO) {
		for (int i = 0; i < listTaskDTO.size(); ++i) {
			for (int j = 0; j < listTaskDTO.size() - 1; ++j) {
				if (listTaskDTO.get(j+1).getStartDate()<listTaskDTO.get(j).getStartDate()) {
					TaskRequireDTO tmp = listTaskDTO.get(j);
					listTaskDTO.set(j, listTaskDTO.get(j + 1));
					listTaskDTO.set(j + 1, tmp);
				}
			}
		}
	}
	@UiHandler("btnBack")
	void onBtnBackClick(ClickEvent event) {
		HMEntrypoint.get().HMHumanReq();
	}

//	@UiHandler("btnRespone")
//	void onBtnResponeClick(ClickEvent event) {
//		HMEntrypoint.get().HMStaffForAddProject(currProject.getProjectID());
//	}
	@UiHandler("lblNumOfOPT")
	void onLblNumOfOPTClick(ClickEvent event) {
		TaskRequireDTO taskReqDTO=new TaskRequireDTO();
		taskReqDTO.setNumOfEmp(currProject.getNumOfOPT());
		taskReqDTO.setStartDate(currProject.getStartDate());
		taskReqDTO.setDueDate(currProject.getDueDate());
		taskReqDTO.setProjectId(currProject.getProjectID());
		HMEntrypoint.get().HMStaffForAddProject(taskReqDTO,currProject);
	}
}
